﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MarginTrading.Entity;
using MarginTrading.Services;
using MarginTrading.Common;
using System.Configuration;

namespace MarginTrading.site
{
    public partial class Login1 : System.Web.UI.Page
    {
        protected void Button2_Click(object sender, EventArgs e)
        {

            if (Username.Text == "" || Password.Text == "")
            {
                labelLoginFailMsg.Text = "Enter Username and Password";
                //MessageBox.Show("Please Enter Username and Password", "Enter Login Details", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            UserAccount User;
            try
            {
                User = UserAccountServices.GetUserAccountByUserName(Username.Text);
            }
            catch (System.Exception ex)
            {
                labelLoginFailMsg.Text = "Site temporally out of service";
                return;
            }

            if (User == null)
            {

                labelLoginFailMsg.Text = "Invalid Username or Password";
                //MessageBox.Show("Invalid Username or Password", "Invalid Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                String sHashedPass = MD5Calculator.CalculateMD5Hash(Password.Text);

                if (sHashedPass == User.Password)
                {
                    //Program.currentUser = User;
                    Session["UserName"] = User.UserName;
                    Session["UserType"] = User.UserType;

                    //if (Request.QueryString["RequestUrl"] != null)
                    //{
                    //    Response.Redirect(Request.QueryString["RequestUrl"].ToString());
                    //}
                    //else
                    //{
                        //Response.Redirect("ClientsLoanAccount.aspx");
                        Response.Redirect("HomePage.aspx");
                    //}

                }
                else
                {
                    labelLoginFailMsg.Text = "Invalid Username or Password";
                    //MessageBox.Show("Invalid Username or Password", "Invalid Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }



        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //Username.Text = "user";
            //Password.Text = "abc123";
            if (ConfigurationSettings.AppSettings["HelpDesk"] != null)
            {
                lblHelpDesk.Text = "Help Desk - " + ConfigurationSettings.AppSettings["HelpDesk"].ToString();
            }
            Master.FindControl("lbtLogOut").Visible = false;
        }
    }
}
